package com.example.boardroom.demo.times;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.LogUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.example.boardroom.R;
import com.example.boardroom.demo.BaseActivity;
import com.example.boardroom.demo.Content;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BasePopupView;
import com.lxj.xpopup.core.CenterPopupView;
import com.lxj.xpopup.interfaces.OnInputConfirmListener;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class TimesActivity2 extends BaseActivity {
    private AdapterTimes2 adapterTimes2;
    RecyclerView recyclerview;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_times2);
        recyclerview = findViewById(R.id.recyclerview);
        initAdapter();
        getData(1, 1, 10);
        initPop();
    }

    private void initAdapter() {
        adapterTimes2 = new AdapterTimes2();
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);
        recyclerview.setLayoutManager(gridLayoutManager);
        recyclerview.setAdapter(adapterTimes2);
        adapterTimes2.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                TimesRootNode.RowsDTO node = (TimesRootNode.RowsDTO) adapter.getData().get(position);
                getData2ByPageName(1, node.packageName, 1, 10);
            }
        });
    }

    /*
    `deviceId` bigint(20) DEFAULT NULL COMMENT '设备ID',
  `handleType` char(1) NOT NULL DEFAULT '0' COMMENT '操作类型（0本设备 1设备里的应用）',
  `packageName` varchar(100) DEFAULT NULL COMMENT '包名称',
  `appName` varchar(50) DEFAULT NULL COMMENT '包对应的应用名称',
pageNum:
1
pageSize:
10
orderByColumn:
create_time
isAsc:
desc

     */
    private void getData(long deviceId, int pageNum, int pageSize) {
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("deviceId", deviceId);
        hashMap.put("handleType", "1");
        hashMap.put("pageNum", pageNum);
        hashMap.put("pageSize", pageSize);
        OkGo.<String>post(Content.baseUrl + "link/deviceUseLog/list")
                .upJson(GsonUtils.toJson(hashMap))
                .tag(this)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        LogUtils.i("lufei_getData", response.body());
                        TimesRootNode timesRootNode = GsonUtils.fromJson(response.body(), TimesRootNode.class);
                        adapterTimes2.setList(timesRootNode.rows);

                    }
                });
    }

    private void getData2ByPageName(long deviceId, String packageName, int pageNum, int pageSize) {
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("deviceId", deviceId);
        hashMap.put("handleType", "1");
        hashMap.put("packageName", packageName);
        hashMap.put("pageNum", pageNum);
        hashMap.put("pageSize", pageSize);
        OkGo.<String>post(Content.baseUrl + "link/deviceUseLog/selectUseLogList")
                .upJson(GsonUtils.toJson(hashMap))
                .tag(this)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        LogUtils.i("lufei_getData2", response.body());
                        BeanTimes2 json = GsonUtils.fromJson(response.body(), BeanTimes2.class);
                        popupView.show();
                        EventBus.getDefault().post(new EventTimes(json.rows));

                    }
                });
    }


    private void setEntity(TimesRootNode timesRootNode) {

    }

    BasePopupView popupView;

    private void initPop() {
        CustomPopup customPopup = new CustomPopup(TimesActivity2.this);
        popupView = new XPopup.Builder(TimesActivity2.this)
                .autoOpenSoftInput(false)
                .asCustom(customPopup);
    }

    RecyclerView recyclerView2;
    private AdapterTimeLitte adapterTimeLitte;

    private void initAdapter2() {
        recyclerView2.setLayoutManager(new LinearLayoutManager(this));
        adapterTimeLitte = new AdapterTimeLitte(new ArrayList<>());
        recyclerView2.setAdapter(adapterTimeLitte);
    }

    public class CustomPopup extends CenterPopupView {
        @Subscribe(threadMode = ThreadMode.MAIN)
        public void getEvent(EventTimes event) {
          if (event.type!=2&&event.type!=3){
              List<BeanTimes2.RowsDTO> rows = (List<BeanTimes2.RowsDTO>) event.msg;
              adapterTimeLitte.setList(rows);
          }
        }


        public CustomPopup(@NonNull Context context) {
            super(context);
            EventBus.getDefault().register(this);
        }

        @Override
        public void onDestroy() {
            super.onDestroy();
            EventBus.getDefault().unregister(this);
        }

        @Override
        protected int getImplLayoutId() {
            return R.layout.custom_popup;
        }

        @Override
        protected void onCreate() {
            super.onCreate();
            recyclerView2 = findViewById(R.id.recyclerview);
            recyclerView2.setLayoutManager(new LinearLayoutManager(getContext()));
            initAdapter2();

        }
//        @Override
//        protected int getMaxHeight() {
//            return 200;
//        }
//
//        @Override
//        protected int getMaxWidth() {
//            return 1000;
//        }
    }


}
